package com.kamistoat.meimeichat.meimeichatclient.event;

import com.alibaba.fastjson.JSON;
import com.kamistoat.meimeichat.meimeichatclient.config.NettyClientConstant;
import com.kamistoat.meimeichat.meimeichatclient.infrastructure.util.BeanUtil;
import com.kamistoat.meimeichat.meimeichatclient.infrastructure.util.CacheUtil;
import com.kamistoat.meimeichat.meimeichatui.view.login.ILoginEvent;
import com.kamistoat.meimeichatserveraggrement.protocol.login.LoginRequest;
import com.kamistoat.meimeichatserveraggrement.protocol.login.dto.ChatRecordDto;
import com.kamistoat.meimeichatserveraggrement.util.R;
import io.netty.channel.Channel;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;

public class LoginEvent implements ILoginEvent {

    private Logger logger = LoggerFactory.getLogger(LoginEvent.class);

    @Override
    public void doLoginCheck(String userId, String userPassword) {
        Channel channel = BeanUtil.getBean("Client2ServerChannel", Channel.class);
        channel.writeAndFlush(new LoginRequest(userId, userPassword));
        CacheUtil.userId = userId;
    }

    @Override
    public String doQueryUserHead(String userId) {
        if (userId == null || userId.equals("")) {
            return null;
        }
        // 发送web请求查询用户头像
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        try {
            httpClient = HttpClientBuilder.create().build();
            URIBuilder uriBuilder = new URIBuilder(
                    NettyClientConstant.NETTY_SERVER_WEB_HOST +
                            ":" +
                            NettyClientConstant.NETTY_SERVER_WEB_PORT +
                            NettyClientConstant.WebApi.QUERY_USER_HEAD_URI.getUri());
            uriBuilder.addParameter("userId", userId);
            HttpGet httpGet = new HttpGet(uriBuilder.build());
            response = httpClient.execute(httpGet);
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                R responseR = JSON.parseObject(EntityUtils.toString(responseEntity), R.class);
                Object userHeadURI = responseR.get("userHeadURI");
                if ((int) responseR.get("code") == 0 && userHeadURI != null && !userHeadURI.toString().equals("")) {
                    return userHeadURI.toString();
                }
            }
        } catch (URISyntaxException | IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}